package com.qifenqian.turbot.module.adapter.marketing

import android.support.v7.widget.GridLayoutManager
import android.view.Gravity
import android.view.Menu
import android.view.View
import android.widget.PopupMenu
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import com.qifenqian.turbot.R
import com.qifenqian.turbot.module.adapter.itemdecoration.GridItemDecoration
import com.qifenqian.turbot.module.ui.marketing.ACTION_FREE_DISCOUNT_EDIT
import com.qifenqian.turbot.module.ui.marketing.FreeDiscountEditActivity
import kotlinx.android.synthetic.main.item_free_time_discount.view.*

/**
 * @author H
 * @createTime 2019/1/3 17:04
 * @description
 * @see
 * @since
 */
class FreeTimeDiscountAdapter(list: List<String>) : BaseQuickAdapter<String, BaseViewHolder>(R.layout.item_free_time_discount, list) {

    var list = mutableListOf("1", "2", "3", "4")
    private var mFreeTimeDiscountChildAdapter: FreeTimeDiscountChildAdapter? = null
    override fun convert(helper: BaseViewHolder?, item: String?) {
        val view = helper!!.itemView
        view.tv_item_free_time_discount_time.text = "时间"
        view.rv_item_free_time_discount.layoutManager = GridLayoutManager(mContext, 3)
        mFreeTimeDiscountChildAdapter = FreeTimeDiscountChildAdapter(mContext, list)
        view.rv_item_free_time_discount.adapter = mFreeTimeDiscountChildAdapter
        view.rv_item_free_time_discount.addItemDecoration(GridItemDecoration(mContext, 16))
        mFreeTimeDiscountChildAdapter?.setOnFreeTimeDiscountClickListener(object : FreeTimeDiscountChildAdapter.OnFreeTimeDiscountClickListener {
            override fun onClick(position: Int, view: View) {
                FreeDiscountEditActivity.startThis(mContext, ACTION_FREE_DISCOUNT_EDIT)
//                showPopupMenu(position, view)
            }
        })
    }

    private fun showPopupMenu(position: Int, view: View) {
        val popupMenu = PopupMenu(mContext, view, Gravity.END)
        popupMenu.menu?.add(Menu.NONE, 0x0, 0, "编辑")
        popupMenu.menu?.add(Menu.NONE, 0x1, 1, "删除")
        popupMenu.setOnMenuItemClickListener {
            when (it.itemId) {
                0x0 -> FreeDiscountEditActivity.startThis(mContext, ACTION_FREE_DISCOUNT_EDIT)
                0x1 -> mFreeTimeDiscountChildAdapter?.remove(position)
            }
            return@setOnMenuItemClickListener true
        }
        popupMenu.show()
    }
}